﻿package view.buffer
{
    import com.core.*;
    import com.core.destroy.*;
    import com.sh.game.global.*;
    import control.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    import morn.core.components.*;
    import plugin.union.*;
    import view.message.*;
    import view.uilt.*;

    public class BuffersViewN extends Sprite
    {
        private var _btn_leftright1:Button;
        private var _btn_leftright2:Button;
        private var container2:Sprite;
        private var itemBuffers:Dictionary;
        private const bob:int = 21;
        private var _inited:Boolean = false;
        private var cfg:Object;
        private var bindBuffarr:Array;
        private const bufferBindDatas:Object;
        private var createAll:Boolean = false;
        private var _VtimeID:uint = 0;
        private var _timePao:Boolean = false;

        public function BuffersViewN()
        {
            this.bufferBindDatas = {weiduan:{type:"bind", id:"weiduan", icon:"weiduan", minlevel:0, maxlevel:0, vip:0, fileExt:"png", index:5}, fashion:{type:"bind", id:"fashion", icon:"fashion", minlevel:0, maxlevel:0, vip:0, fileExt:"png", index:3}, tuteng:{type:"bind", id:"tuteng", icon:"union", minlevel:0, maxlevel:0, vip:0, fileExt:"png", index:4}, vip:{type:"bind", id:"vip", icon:"vip", minlevel:0, maxlevel:0, vip:1, fileExt:"png", index:1}, fashen:{type:"bind", id:"fashen", icon:"fashen", minlevel:0, maxlevel:0, vip:0, fileExt:"png", index:2}, worldlevel:{type:"bind", id:"worldlevel", icon:"worldlevel", minlevel:60, maxlevel:0, vip:0, index:6, fileExt:"png"}, vplan:{type:"bind", id:"vplan", icon:"vtequan", minlevel:0, maxlevel:0, vip:0, index:7, fileExt:"png"}, shuangbei:{type:"bind", id:"shuangbei", icon:"shuangbei", minlevel:0, maxlevel:0, vip:0, index:7, fileExt:"png"}, shuangbeidaguai:{type:"bind", id:"shuangbeidaguai", icon:"shuangbei", minlevel:0, maxlevel:0, vip:0, index:7, fileExt:"png"}};
            this._btn_leftright1 = new Button("png.common.btn_lred");
            this._btn_leftright2 = new Button("png.common.btn_lred");
            this.container2 = new Sprite();
            this.addChild(this.container2);
            this._btn_leftright2.y = 48 - 14;
            this.container2.x = 40;
            this.container2.y = 0;
            this._btn_leftright1.addEventListener(MouseEvent.CLICK, this.clickHandler);
            this._btn_leftright2.addEventListener(MouseEvent.CLICK, this.clickHandler2);
            this.itemBuffers = new Dictionary();
            var _loc_1:* = new BuffersControl(this);
            ModelProxy.addObserver("bufferView", _loc_1);
            this._btn_leftright1.scaleX = 0.65;
            this._btn_leftright1.scaleY = 0.65;
            this._btn_leftright2.scaleX = 0.65;
            this._btn_leftright2.scaleY = 0.65;
            return;
        }// end function

        protected function clickHandler(event:MouseEvent) : void
        {
            return;
        }// end function

        protected function clickHandler2(event:MouseEvent) : void
        {
            this.container2.visible = !this.container2.visible;
            if (this.container2.visible == true)
            {
                this._btn_leftright2.scaleX = 1;
                this._btn_leftright2.x = 0;
            }
            else
            {
                this._btn_leftright2.scaleX = -1;
                this._btn_leftright2.x = 16 * 0.65;
            }
            return;
        }// end function

        private function clear() : void
        {
            var _loc_1:* = this.container2.numChildren - 1;
            while (_loc_1 >= 0)
            {
                
                this.container2.removeChildAt(0);
                _loc_1 = _loc_1 - 1;
            }
            DestroyUtil.destroyMap(this.itemBuffers);
            this.updatebtn();
            return;
        }// end function

        public function initBuffers(param1:Boolean = false) : void
        {
            var _loc_2:Object = null;
            var _loc_3:Number = NaN;
            var _loc_4:Array = null;
            var _loc_5:Object = null;
            var _loc_6:Object = null;
            var _loc_7:Object = null;
            var _loc_8:int = 0;
            this.cfg = ConfigDictionary.data.buffer;
            if (!this._inited || param1)
            {
                this.clear();
                this.createAllBindBuffer();
                _loc_2 = UserData.getUserData();
                _loc_3 = ModelProxy.getValue("data", "servertime") + getTimer();
                if (_loc_2 && _loc_2.hasOwnProperty("beBuffers"))
                {
                    _loc_4 = [];
                    for (_loc_5 in _loc_2.beBuffers)
                    {
                        
                        _loc_8 = -1;
                        if (_loc_2.beBuffers[_loc_5] && _loc_2.beBuffers[_loc_5].removetime > 0)
                        {
                            _loc_8 = int((_loc_2.beBuffers[_loc_5].removetime - _loc_3) / 1000);
                            if (_loc_8 < 0)
                            {
                                _loc_4.push(_loc_5);
                                continue;
                            }
                        }
                        this.addSkillBuffer({bufferid:_loc_5, dispelTime:_loc_8});
                    }
                    for each (_loc_6 in _loc_4)
                    {
                        
                        _loc_2.beBuffers[_loc_6] = null;
                        delete _loc_2.beBuffers[_loc_6];
                    }
                    for each (_loc_7 in _loc_2.itemBuffers)
                    {
                        
                        for (_loc_6 in _loc_7.items)
                        {
                            
                            this.addItemBuffer([_loc_6, _loc_7.items[_loc_6]], false);
                        }
                    }
                    this._inited = true;
                }
            }
            this.updatebtn();
            return;
        }// end function

        public function removeItemBuffer(param1:Object) : void
        {
            var _loc_3:int = 0;
            var _loc_4:BufferItemN = null;
            var _loc_2:* = param1[1];
            if (this.itemBuffers[_loc_2])
            {
                if (this.itemBuffers[_loc_2].parent)
                {
                    this.itemBuffers[_loc_2].parent.removeChild(this.itemBuffers[_loc_2]);
                    this.itemBuffers[_loc_2].dispose();
                    delete this.itemBuffers[_loc_2];
                    _loc_3 = 0;
                    for each (_loc_4 in this.itemBuffers)
                    {
                        
                        _loc_4.setX = _loc_3 * this.bob;
                        _loc_3++;
                    }
                }
                this.updatebtn();
            }
            return;
        }// end function

        public function removeSkillBuffer(param1:int) : void
        {
            var _loc_2:int = 0;
            var _loc_3:BufferItemN = null;
            if (this.itemBuffers[param1])
            {
                if (this.itemBuffers[param1].parent)
                {
                    this.itemBuffers[param1].parent.removeChild(this.itemBuffers[param1]);
                    this.itemBuffers[param1].dispose();
                    delete this.itemBuffers[param1];
                    _loc_2 = 0;
                    for each (_loc_3 in this.itemBuffers)
                    {
                        
                        _loc_3.setX = _loc_2 * this.bob;
                        _loc_2++;
                    }
                }
            }
            this.updatebtn();
            if (param1 == 10610 || param1 == 10710)
            {
                ModelProxy.change("mainui", "removeLiehuo", param1);
            }
            return;
        }// end function

        public function addSkillBuffer(param1:Object) : void
        {
            var _loc_3:Object = null;
            var _loc_4:BufferItemN = null;
            var _loc_5:Number = NaN;
            if (param1.bufferid == 10610 || param1.bufferid == 10710)
            {
                if (UserData.getUserData("career") != 1)
                {
                    if (Config.debug)
                    {
                        Alert2.show("出现于职业不符合的技能buff,请确认服务端信息是否正常,bufferid:" + param1.bufferid, "测试异常错误", ["确定"]);
                    }
                    else
                    {
                        return;
                    }
                }
            }
            var _loc_2:* = this.cfg[param1.bufferid];
            if (_loc_2 && _loc_2.show == 1)
            {
                if (this.itemBuffers[param1.bufferid])
                {
                    this.removeSkillBuffer(param1.bufferid);
                }
                _loc_3 = {type:0, id:param1.bufferid, dispelTime:param1.dispelTime};
                _loc_4 = new BufferItemN(_loc_3);
                _loc_5 = this.container2.numChildren * this.bob;
                _loc_4.setX = _loc_5;
                this.container2.addChild(_loc_4);
                _loc_4.showMove(this.updatebtn);
                this.itemBuffers[param1.bufferid] = _loc_4;
            }
            this.updatebtn();
            if (param1.bufferid == 10610 || param1.bufferid == 10710)
            {
                ModelProxy.change("mainui", "addLiehuo", {cfg:_loc_2, msg:param1});
            }
            return;
        }// end function

        public function addItemBuffer(param1:Object, param2:Boolean = true) : void
        {
            var _loc_3:int = 0;
            if (param1[1].hasOwnProperty("dispelTime"))
            {
                _loc_3 = param1[1].dispelTime;
            }
            else
            {
                _loc_3 = param1[1].totaltime - param1[1].costtime;
            }
            var _loc_4:Object = {type:param1[1].type, id:param1[0], dispelTime:_loc_3, timeout:param1[1].timeout};
            if (this.itemBuffers[_loc_4.id])
            {
                this.removeItemBuffer([0, _loc_4.id]);
            }
            var _loc_5:* = new BufferItemN(_loc_4);
            this.itemBuffers[_loc_4.id] = _loc_5;
            this.container2.addChild(_loc_5);
            this.updatebtn();
            if (param2)
            {
                _loc_5.showMove(this.updatebtn);
            }
            return;
        }// end function

        private function updatebtn() : void
        {
            var _loc_4:int = 0;
            var _loc_5:String = null;
            var _loc_6:BufferItemN = null;
            var _loc_1:* = new Array();
            var _loc_2:* = new Array();
            var _loc_3:Array = [];
            if (this.container2.numChildren > 0)
            {
                _loc_4 = 0;
                while (_loc_4 < this.container2.numChildren)
                {
                    
                    _loc_6 = this.container2.getChildAt(_loc_4) as BufferItemN;
                    if (_loc_6.buffer && _loc_6.buffer.type == "bind")
                    {
                        _loc_2.push({buf:_loc_6, index:_loc_6.buffer.index});
                    }
                    else
                    {
                        _loc_3.push(_loc_6);
                    }
                    _loc_4++;
                }
            }
            _loc_2.sortOn("index", Array.NUMERIC);
            if (this.container2.numChildren > 0)
            {
                _loc_4 = 0;
                while (_loc_4 < this.container2.numChildren)
                {
                    
                    if (_loc_2[_loc_4])
                    {
                        _loc_2[_loc_4].buf.setX = _loc_4 * this.bob;
                    }
                    else if (_loc_3[_loc_4 - _loc_2.length])
                    {
                        _loc_3[_loc_4 - _loc_2.length].setX = _loc_4 * this.bob;
                    }
                    _loc_4++;
                }
            }
            for (_loc_5 in this.itemBuffers)
            {
                
                if (!this.bufferBindDatas.hasOwnProperty(_loc_5))
                {
                    _loc_1.push(_loc_5);
                }
            }
            ModelProxy.change("data", "user.itemBuffersarr", _loc_1);
            this._btn_leftright2.visible = this.container2.numChildren > 0;
            return;
        }// end function

        public function removeAllRingBuffer() : void
        {
            var _loc_1:int = 999982;
            while (_loc_1 < 999997)
            {
                
                if (this.itemBuffers[_loc_1])
                {
                    if (this.itemBuffers[_loc_1].parent)
                    {
                        this.itemBuffers[_loc_1].parent.removeChild(this.itemBuffers[_loc_1]);
                        this.itemBuffers[_loc_1].dispose();
                        delete this.itemBuffers[_loc_1];
                    }
                }
                _loc_1++;
            }
            this.updatebtn();
            return;
        }// end function

        public function removeAllFashionBuffer() : void
        {
            var _loc_1:int = 350022;
            while (_loc_1 < 350040)
            {
                
                if (this.itemBuffers[_loc_1])
                {
                    if (this.itemBuffers[_loc_1].parent)
                    {
                        this.itemBuffers[_loc_1].parent.removeChild(this.itemBuffers[_loc_1]);
                        this.itemBuffers[_loc_1].dispose();
                        delete this.itemBuffers[_loc_1];
                    }
                }
                _loc_1++;
            }
            this.updatebtn();
            return;
        }// end function

        public function swapeRingBuffer(param1:Object) : void
        {
            this.removeAllRingBuffer();
            this.addItemBuffer(param1);
            return;
        }// end function

        public function swapeFashionBuffer(param1:Object) : void
        {
            this.removeAllFashionBuffer();
            if (param1 != null)
            {
                this.addItemBuffer(param1);
            }
            return;
        }// end function

        public function juling(param1:int) : void
        {
            var _loc_2:Object = {id:240502001, count:param1, build:"juling"};
            if (param1 > 0)
            {
                this.addjuling(_loc_2);
            }
            else if (this.itemBuffers[_loc_2.id])
            {
                this.removeItemBuffer([0, _loc_2.id]);
            }
            return;
        }// end function

        public function addjuling(param1:Object) : void
        {
            if (this.itemBuffers[param1.id])
            {
                this.removeItemBuffer([0, param1.id]);
            }
            var _loc_2:* = new BufferItemN(param1);
            this.container2.addChild(_loc_2);
            _loc_2.showMove(this.updatebtn);
            this.itemBuffers[param1.id] = _loc_2;
            return;
        }// end function

        public function createAllBindBuffer() : void
        {
            var _loc_4:int = 0;
            var _loc_5:String = null;
            var _loc_6:Object = null;
            var _loc_7:Array = null;
            var _loc_8:Array = null;
            var _loc_9:Object = null;
            var _loc_10:Array = null;
            var _loc_11:int = 0;
            var _loc_12:int = 0;
            var _loc_13:Object = null;
            var _loc_14:Object = null;
            var _loc_15:int = 0;
            var _loc_16:int = 0;
            var _loc_17:Object = null;
            var _loc_18:Object = null;
            var _loc_19:Date = null;
            var _loc_20:Date = null;
            var _loc_21:Date = null;
            var _loc_1:* = UserData.getUserData("level");
            var _loc_2:* = ModelProxy.getValue("vip", "getvip");
            var _loc_3:* = _loc_2 ? (_loc_2.vipLevel) : (0);
            for (_loc_5 in this.bufferBindDatas)
            {
                
                _loc_6 = this.bufferBindDatas[_loc_5];
                if (_loc_6.minlevel > 0 && _loc_1 < _loc_6.minlevel && _loc_6.minlevel != 0)
                {
                    this.removeBufferItem(_loc_5);
                    continue;
                }
                if (_loc_6.maxlevel > 0 && _loc_1 > _loc_6.maxlevel && _loc_6.maxlevel != 0)
                {
                    this.removeBufferItem(_loc_5);
                    continue;
                }
                if (_loc_6.vip > 0 && ItemUilt.checkVip() == false)
                {
                    this.removeBufferItem(_loc_5);
                    continue;
                }
                if (_loc_6.id == "fashion")
                {
                    _loc_7 = ModelProxy.getValue("data", "fashion");
                    if (_loc_7 && _loc_7.length > 0)
                    {
                        this.addItemBindBuffer(_loc_5, this.createAll);
                    }
                    continue;
                }
                if (_loc_6.id == "vip")
                {
                    if (_loc_3 > 0)
                    {
                        this.addItemBindBuffer(_loc_5, this.createAll);
                    }
                    continue;
                }
                if (_loc_6.id == "fashen")
                {
                    _loc_8 = ModelProxy.getValue("data", "fashen");
                    for each (_loc_9 in _loc_8)
                    {
                        
                        if (_loc_9.level > 0)
                        {
                            this.addItemBindBuffer(_loc_5, this.createAll);
                        }
                    }
                    continue;
                }
                if (_loc_6.id == "tuteng")
                {
                    if (UserData.getUserData("unionid") > 0)
                    {
                        _loc_10 = ModelProxy.getValue("data", "unionTotem");
                        if (_loc_10)
                        {
                            for each (_loc_11 in _loc_10)
                            {
                                
                                if (_loc_11 > 0)
                                {
                                    this.addItemBindBuffer(_loc_5, this.createAll);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            UnionSender._31getTotem();
                        }
                    }
                    continue;
                }
                if (_loc_6.id == "worldlevel")
                {
                    _loc_12 = int(ModelProxy.getValue("data", "worldlevel"));
                    _loc_13 = UserData.getUserData("level");
                    if (_loc_12 > _loc_13 + 10)
                    {
                        this.addItemBindBuffer(_loc_5, this.createAll);
                    }
                    else
                    {
                        this.removeBufferItem(_loc_5);
                    }
                    continue;
                }
                if (_loc_6.id == "weiduan")
                {
                    if (Config.gameParams.client == 2)
                    {
                        this.addItemBindBuffer(_loc_5, this.createAll);
                    }
                    continue;
                }
                if (_loc_6.id == "vplan")
                {
                    if (this._VtimeID > 0)
                    {
                        clearTimeout(this._VtimeID);
                        this._VtimeID = 0;
                    }
                    _loc_14 = ModelProxy.getValue("data", "vplanbuffer");
                    if (_loc_14 && _loc_14.state != 0 && _loc_14.hasOwnProperty("endTime"))
                    {
                        _loc_4 = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
                        if (_loc_4 >= _loc_14.endTime)
                        {
                            this.removeBufferItem(_loc_5);
                            _loc_14.state = 0;
                        }
                        else
                        {
                            this.addItemBindBuffer(_loc_5, this.createAll);
                            this._VtimeID = setTimeout(this.removeVplan, (_loc_14.endTime - _loc_4) * 1000);
                        }
                    }
                    continue;
                }
                if (_loc_6.id == "shuangbei")
                {
                    _loc_15 = ModelProxy.getValue("data", "openday") + 1;
                    _loc_16 = ModelProxy.getValue("data", "hefuday") + 1;
                    _loc_17 = ConfigDictionary.data.activitytime;
                    for each (_loc_18 in _loc_17)
                    {
                        
                        if (_loc_18.page == "shuangbei" || _loc_18.page == "shuangbeidaguai1")
                        {
                            switch(int(_loc_18.timetype))
                            {
                                case 1:
                                {
                                    if (_loc_15 >= _loc_18.opentime && _loc_15 <= _loc_18.overtime)
                                    {
                                        this.addItemBindBuffer(_loc_5, this.createAll);
                                    }
                                    break;
                                }
                                case 2:
                                {
                                    _loc_4 = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
                                    if (_loc_4 >= _loc_18.opentimestr / 1000 && _loc_4 <= _loc_18.overtimestr / 1000)
                                    {
                                        this.addItemBindBuffer(_loc_5, this.createAll);
                                    }
                                    break;
                                }
                                case 3:
                                {
                                    if (_loc_16 >= _loc_18.opentime && _loc_16 <= _loc_18.overtime)
                                    {
                                        this.addItemBindBuffer(_loc_5, this.createAll);
                                    }
                                }
                                case 5:
                                {
                                    _loc_4 = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
                                    _loc_19 = new Date();
                                    _loc_19.setTime(_loc_4 * 1000);
                                    _loc_20 = new Date(_loc_19.fullYear, _loc_19.month, _loc_19.date, 23, 59, 59, 59);
                                    _loc_21 = new Date();
                                    _loc_21.setTime(_loc_20.getTime() - (_loc_15 - 1) * 86400000);
                                    if (_loc_21.getTime() >= _loc_18.sopentimestr && _loc_21.getTime() <= _loc_18.sovertimestr)
                                    {
                                        if (_loc_15 >= _loc_18.opentime && _loc_15 <= _loc_18.overtime)
                                        {
                                            this.addItemBindBuffer(_loc_5, this.createAll);
                                        }
                                    }
                                    else if (_loc_4 >= _loc_18.opentimestr / 1000 && _loc_4 <= _loc_18.overtimestr / 1000)
                                    {
                                        this.addItemBindBuffer(_loc_5, this.createAll);
                                    }
                                    break;
                                }
                                default:
                                {
                                    break;
                                }
                            }
                        }
                    }
                    continue;
                }
                if (_loc_6.id == "shuangbeidaguai")
                {
                    _loc_15 = ModelProxy.getValue("data", "openday") + 1;
                    _loc_16 = ModelProxy.getValue("data", "hefuday") + 1;
                    _loc_17 = ConfigDictionary.data.activitytime;
                    for each (_loc_18 in _loc_17)
                    {
                        
                        if (_loc_18.page == "shuangbeidaguai")
                        {
                            switch(int(_loc_18.timetype))
                            {
                                case 1:
                                {
                                    if (_loc_15 >= _loc_18.opentime && _loc_15 <= _loc_18.overtime)
                                    {
                                        this.addItemBindBuffer(_loc_5, this.createAll);
                                    }
                                    break;
                                }
                                case 2:
                                {
                                    _loc_4 = int((ModelProxy.getValue("data", "servertime") + Number(getTimer())) / 1000);
                                    if (_loc_4 >= _loc_18.opentimestr / 1000 && _loc_4 <= _loc_18.overtimestr / 1000)
                                    {
                                        this.addItemBindBuffer(_loc_5, this.createAll);
                                    }
                                    break;
                                }
                                case 3:
                                {
                                    if (_loc_16 >= _loc_18.opentime && _loc_16 <= _loc_18.overtime)
                                    {
                                        this.addItemBindBuffer(_loc_5, this.createAll);
                                    }
                                }
                                default:
                                {
                                    break;
                                }
                            }
                        }
                    }
                    continue;
                }
                this.addItemBindBuffer(_loc_5, this.createAll);
            }
            this.createAll = true;
            return;
        }// end function

        private function removeVplan() : void
        {
            this.removeBufferItem("vplan");
            var _loc_1:* = ModelProxy.getValue("data", "vplanbuffer");
            if (_loc_1)
            {
                _loc_1.state = 0;
            }
            return;
        }// end function

        public function addItemBindBuffer(param1:String, param2:Boolean = false) : void
        {
            var _loc_4:BufferItemN = null;
            var _loc_3:* = this.bufferBindDatas[param1];
            if (_loc_3 == null)
            {
                return;
            }
            if (!this.itemBuffers[_loc_3.id])
            {
                _loc_4 = new BufferItemN(_loc_3);
                this.container2.addChildAt(_loc_4, 0);
                this.updatebtn();
                if (param2)
                {
                    _loc_4.showMove(this.updatebtn);
                }
                this.itemBuffers[_loc_3.id] = _loc_4;
            }
            return;
        }// end function

        public function removeBufferItem(param1:String) : void
        {
            if (this.itemBuffers[param1])
            {
                if (this.itemBuffers[param1].parent)
                {
                    this.itemBuffers[param1].parent.removeChild(this.itemBuffers[param1]);
                    this.itemBuffers[param1].dispose();
                    delete this.itemBuffers[param1];
                }
            }
            this.updatebtn();
            return;
        }// end function

        public function get timePao() : Boolean
        {
            return this._timePao;
        }// end function

        public function set timePao(param1:Boolean) : void
        {
            if (param1 == false && this._timePao)
            {
                this.removeBufferItem("pao");
            }
            this._timePao = param1;
            return;
        }// end function

        public function showPao(param1:Boolean) : void
        {
            if (param1 && this.timePao)
            {
                if (this.itemBuffers["pao"])
                {
                    return;
                }
                this.addItemBindBuffer("pao", true);
            }
            else
            {
                this.removeBufferItem("pao");
            }
            return;
        }// end function

    }
}
